﻿Imports Microsoft.Xna.Framework

Namespace Global.Runa.UI.Metadata

    Public MustInherit Class VectorConverter
        Inherits TypeConverter

        Public Sub New(g As Game)
            MyBase.New(g)
        End Sub

        Public Overrides Function ConvertFromInvariantString(text As String) As Object
            If text.Contains(",") Then
                Dim fields = text.Split(",")
                For i As Integer = 0 To fields.Length - 1
                    fields(i) = fields(i).Trim()
                Next
                If fields.Length >= 2 Then
                    Select Case fields.Length
                        Case 2
                            Return New Vector2(Single.Parse(fields(0)), Single.Parse(fields(1)))
                        Case 3
                            Return New Vector3(Single.Parse(fields(0)), Single.Parse(fields(1)), Single.Parse(fields(2)))
                        Case 4
                            Return New Vector4(Single.Parse(fields(0)), Single.Parse(fields(1)), Single.Parse(fields(2)), Single.Parse(fields(3)))
                        Case Else
                            Throw New ArgumentException("Not valid value for this property.")
                    End Select
                Else
                    Throw New ArgumentException("Not valid value for this property.")
                End If
            Else
                Throw New ArgumentException("Not valid value for this property.")
            End If
        End Function

        Public Overrides Function ConvertFromString(text As String) As Object
            Return ConvertFromInvariantString(text)
        End Function

    End Class

    Public Class Vector2Converter
        Inherits VectorConverter

        Public Sub New(g As Game)
            MyBase.New(g)
        End Sub

        Public Overrides Function ConvertToInvariantString(value As Object) As String
            If value.GetType() IsNot GetType(Vector2) Then
                Throw New ArgumentException("Not valid value for this property.")
            Else
                With CType(value, Vector2)
                    Return String.Format("{0}, {1}", .X, .Y)
                End With
            End If
        End Function

        Public Overrides Function ConvertToString(value As Object) As String
            Return ConvertToInvariantString(value)
        End Function

        Public Overrides Function CreateInstance(propertyValues As IDictionary) As Object
            Return New Vector2(Single.Parse(propertyValues.Values(0).ToString()), Single.Parse(propertyValues.Values(1).ToString()))
        End Function

        Public Overrides Function GetCreateInstanceSupported() As Boolean
            Return True
        End Function

        Public Overrides Function GetPropertiesSupported() As Boolean
            Return False
        End Function

        Public Overrides Function GetStandardValuesSupported() As Boolean
            Return False
        End Function

    End Class

    Public Class Vector3Converter
        Inherits VectorConverter

        Public Sub New(g As Game)
            MyBase.New(g)
        End Sub

        Public Overrides Function ConvertToInvariantString(value As Object) As String
            If value.GetType() IsNot GetType(Vector3) Then
                Throw New ArgumentException("Not valid value for this property.")
            Else
                With CType(value, Vector3)
                    Return String.Format("{0}, {1}, {2}", .X, .Y, .Z)
                End With
            End If
        End Function

        Public Overrides Function ConvertToString(value As Object) As String
            Return ConvertToInvariantString(value)
        End Function

        Public Overrides Function CreateInstance(propertyValues As IDictionary) As Object
            Return New Vector3(Single.Parse(propertyValues.Values(0).ToString()), Single.Parse(propertyValues.Values(1).ToString()), Single.Parse(propertyValues.Values(2).ToString()))
        End Function

        Public Overrides Function GetCreateInstanceSupported() As Boolean
            Return True
        End Function

        Public Overrides Function GetPropertiesSupported() As Boolean
            Return False
        End Function

        Public Overrides Function GetStandardValuesSupported() As Boolean
            Return False
        End Function

    End Class

    Public Class Vector4Converter
        Inherits VectorConverter

        Public Sub New(g As Game)
            MyBase.New(g)
        End Sub

        Public Overrides Function ConvertToInvariantString(value As Object) As String
            If value.GetType() IsNot GetType(Vector4) Then
                Throw New ArgumentException("Not valid value for this property.")
            Else
                With CType(value, Vector4)
                    Return String.Format("{0}, {1}, {2}, {3}", .X, .Y, .Z, .W)
                End With
            End If
        End Function

        Public Overrides Function ConvertToString(value As Object) As String
            Return ConvertToInvariantString(value)
        End Function

        Public Overrides Function CreateInstance(propertyValues As IDictionary) As Object
            Dim lst As New List(Of Single)
            For i As Integer = 0 To 3
                lst.Add(Single.Parse(propertyValues.Values(i).ToString()))
            Next
            Return New Vector4(lst(0), lst(1), lst(2), lst(3))
        End Function

        Public Overrides Function GetCreateInstanceSupported() As Boolean
            Return True
        End Function

        Public Overrides Function GetPropertiesSupported() As Boolean
            Return False
        End Function

        Public Overrides Function GetStandardValuesSupported() As Boolean
            Return False
        End Function

    End Class

End Namespace